import request from '@/utils/request'

/**
 * 查询角色列表
 */
export function getRoleList(query?: any) {
  return request.get('/system/role/list', query)
}

/**
 * 查询角色详细
 */
export function getRole(roleId: number) {
  return request.get(`/system/role/${roleId}`)
}

/**
 * 新增角色
 */
export function addRole(data: any) {
  return request.post('/system/role', data, {
    showLoading: true,
    showSuccess: true,
    successMsg: '角色新增成功',
  })
}

/**
 * 修改角色
 */
export function updateRole(data: any) {
  return request.put('/system/role', data, {
    showLoading: true,
    showSuccess: true,
    successMsg: '角色修改成功',
  })
}

/**
 * 角色数据权限
 */
export function dataScope(data: any) {
  return request.put('/system/role/dataScope', data, {
    showLoading: true,
    showSuccess: true,
    successMsg: '设置成功',
  })
}

/**
 * 角色状态修改
 */
export function changeRoleStatus(roleId: number, status: string) {
  return request.put('/system/role/changeStatus', {
    roleId,
    status,
  }, {
    showLoading: true,
    showSuccess: true,
    successMsg: '状态修改成功',
  })
}

/**
 * 删除角色
 */
export function delRole(roleIds: number | number[]) {
  const ids = Array.isArray(roleIds) ? roleIds.join(',') : roleIds
  return request.delete(`/system/role/${ids}`, {}, {
    showLoading: true,
    showSuccess: true,
    successMsg: '角色删除成功',
  })
}

/**
 * 查询角色已授权用户列表
 */
export function allocatedUserList(query: any) {
  return request.get('/system/role/authUser/allocatedList', query)
}

/**
 * 查询角色未授权用户列表
 */
export function unallocatedUserList(query: any) {
  return request.get('/system/role/authUser/unallocatedList', query)
}

/**
 * 取消用户授权角色
 */
export function authUserCancel(data: any) {
  return request.put('/system/role/authUser/cancel', data, {
    showLoading: true,
    showSuccess: true,
    successMsg: '取消授权成功',
  })
}

/**
 * 批量取消用户授权角色
 */
export function authUserCancelAll(data: any) {
  return request.put('/system/role/authUser/cancelAll', data, {
    showLoading: true,
    showSuccess: true,
    successMsg: '取消授权成功',
  })
}

/**
 * 授权用户选择
 */
export function authUserSelectAll(data: any) {
  return request.put('/system/role/authUser/selectAll', data, {
    showLoading: true,
    showSuccess: true,
    successMsg: '授权成功',
  })
}

/**
 * 根据角色ID查询部门树结构
 */
export function deptTreeSelect(roleId: number) {
  return request.get(`/system/role/deptTree/${roleId}`)
}

/**
 * 导出角色
 */
export function exportRole(query?: any) {
  return request.post('/system/role/export', query, {
    responseType: 'blob',
    showLoading: true,
  })
}
